<?php
/**
* This is a simple script which should request recent bugs from Mantis and add them to a todolist in basecamp
* you would probably run this from a daily cron
* ----------------------------------------------------------- [info]
* @version 0.1
* @created 2008.11.13
* @author alan@zeroasterisk.com Alan Blount - http://zeroasterisk.com/
* ----------------------------------------------------------- [inputs]
* @param string $date_start strtotime() input [-1 day]
* @param int $basecamp_lodolist_id [1234567]
* @param string $basecamp_domain [myproject.updatelog.com]
* @param string $basecamp_user [username]
* @param string $basecamp_pass [********]
* @param string $mantis_path [] usually left blank, this defaults to this domain
* -----------------------------------------------------------
* @return string
*/
# -- config (get from mantis)
include_once('./config_inc.php');
$defaults = array(
	'basecamp_lodolist_id' => 1234567,
	'basecamp_domain' => 'myproject.updatelog.com',
	'basecamp_user' => 'username',
	'basecamp_pass' => 'password',
	'mantis_path' => null, /* blank is good, we auto-get from current path */
	'date_start' => '-1 day',
);
# -- inputs
$date_start = (isset($_GET['date_start']) && !empty($_GET['date_start']) ? $_GET['date_start'] : $defaults['date_start']);
$basecamp_lodolist_id = (isset($_GET['basecamp_lodolist_id']) && !empty($_GET['basecamp_lodolist_id']) ? $_GET['basecamp_lodolist_id'] : $defaults['basecamp_lodolist_id']);
$basecamp_domain = (isset($_GET['basecamp_domain']) && !empty($_GET['basecamp_domain']) ? $_GET['basecamp_domain'] : $defaults['basecamp_domain']);
$basecamp_user = (isset($_GET['basecamp_user']) && !empty($_GET['basecamp_user']) ? $_GET['basecamp_user'] : $defaults['basecamp_user']);
$basecamp_pass = (isset($_GET['basecamp_pass']) && !empty($_GET['basecamp_pass']) ? $_GET['basecamp_pass'] : $defaults['basecamp_pass']);
$mantis_path = (isset($_GET['mantis_path']) && !empty($_GET['mantis_path']) ? $_GET['mantis_path'] : $defaults['mantis_path']);
# -- setup
$basecamp_url = "https://$basecamp_domain/todo_lists/$basecamp_lodolist_id/todo_items.xml";
if (empty($mantis_path)) {
	$path_parts = explode('/',$_SERVER['REQUEST_URI']);
	array_pop($path_parts);
	$mantis_path = 'http://'.$_SERVER['HTTP_HOST'].'/'.implode('/',$path_parts).'view.php?id=';
}
# -- get from manits
$g_hostname = 'db';
if ($g_db_type!='mysql') {
	die('Only works with Mysql database right now');
}
$mysql_link = mysql_connect($g_hostname, $g_db_username, $g_db_password) or die('Could not connect: ' . mysql_error());
mysql_select_db($g_database_name) or die('Could not select database');
$sql = 'SELECT b.`id`,b.`date_submitted`,b.`summary`,p.`name` 
FROM `mantis_bug_table` as b
LEFT JOIN `mantis_project_table` AS p on (b.`project_id` = p.`id`)
WHERE b.`status` < 80 AND b.`date_submitted` > "'.date('Y-m-d',strtotime($date_start)).'";';
$result = mysql_query($sql);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sql;
    die($message);
}
# -- make mantis bugs
while ($row = mysql_fetch_assoc($result)) {
	$content = "<a href=\"$basecamp_url\">#{$row['id']}</a> [{$row['name']}] {$row['summary']} <i><small>Created: {$row['date_submitted']}</small></i>";
	$xml = "<todo-item><content><![CDATA[$content]]></content></todo-item>";
	echo "<br/>Making todo for: $content";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_URL, $basecamp_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_USERPWD, "$basecamp_user:$basecamp_pass");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HTTPHEADER,array(
		"MIME-Version: 1.0",
		"Accept: application/xml",
		"Content-type: application/xml",
	));
	$re = curl_exec($ch);
	curl_close($ch);
}
echo "<hr/>done.";
?>